'use strict';
const BaseController = require('./base');
const { addRules, updateRules, hideRules } = require('../validator/userQuestion');
class UserQuestion extends BaseController {

  // 新增题目问答
  async add() {
    const { ctx, service, app } = this;
    const errors = app.validator.validate(addRules, ctx.request.body);
    errors && ctx.helper.commonErr('参数错误', { beans: errors });
    const user_id = ctx.helper.decodeJwt(ctx);
    const data = ctx.request.body;
    const res = await service.userQuestion.add({user_id, ...data});
    ctx.helper.success({ bean: res }, '新增问答成功');
  }

  // 查询
  async query() {
    const { ctx, service, app } = this;
    const user_id = ctx.helper.decodeJwt(ctx);
    const res = await service.userQuestion.query(user_id);
    ctx.helper.success({ beans: res }, '查询问答成功');
  }

  // 编辑话题
  async update() {
    const { ctx, service, app } = this;
    const errors = app.validator.validate(updateRules, ctx.request.body);
    errors && ctx.helper.commonErr('参数错误', { beans: errors });
    const { id, content } = ctx.request.body;
    const res = await service.userQuestion.update(id, content);
    ctx.helper.success({ bean: res }, '编辑问答成功');
  }

  // 隐藏话题
  async hide() {
    const { ctx, service, app } = this;
    const errors = app.validator.validate(hideRules, ctx.request.body);
    errors && ctx.helper.commonErr('参数错误', { beans: errors });
    const { id } = ctx.request.body;
    await service.userQuestion.hide(id, ctx.helper.time());
    ctx.helper.success({}, '隐藏问答成功');
  }
}

module.exports = UserQuestion;
